<?php

namespace App\Controller;

use Think\Controller;

/**
 * 
 * 文件用途描述: 支付回调
 * @authors xiaohui
 * @date    2017-12-27 10:05:35
 */
class PayCallbackController extends Controller 
{
    
    /**
     * [aliPayCallback]
     * 函数用途描述: 支付宝 支付回调处理
     * @date:  2017-08-04
     * @author: xiaohui
     * @param: [variable]
     * @return [type]     [description]
     */
    public function aliPayCallback() 
    {
        // 设置 支付宝配置
        C('RSA_PRIVATE_KEY','Alikey/rsa_private_key.pem');
        C('RSA_PUBLIC_KEY','Alikey/rsa_public_key.pem');
        C('CACERT',getcwd().'\\Alikey\\cacert.pem');
        C('PARTNER','2088721791109874');
        // 判断回调 
        $data = $_POST;
        file_put_contents('alipay.txt', json_encode($data));
        if ($data['trade_status'] == "TRADE_SUCCESS") {
            switch($data['body']){
                // 充值
                case 'recharge':
                    $status = D('Pay')->aliPayRecharge($data);;
                    break;
                // 购买置顶套餐
                case 'top_package':
                    $status = D('Pay')->aliPayTopPackage($data);;
                    break;
                default:
                    $status = false;
            }
        } else {
            $status = false;
        }

        if($status == 200){
            echo('success');
        }else{
            echo('failed');
        }
    }

    /**
     * [wxPayCallback]
     * 函数用途描述: 微信 支付回调处理
     * @date:  2017-08-04
     * @author: xiaohui
     * @param: [variable]
     * @return [type]     [description]
     */
    public function wxPayCallback() 
    {
        $trade_type ='APP';
        $wx_class = new \Org\Util\Wxpay(C('WX_APP_ID'),C('WX_SECRET'),C('WX_MCH_ID'),C('WX_PAY_KEY'),C('WX_PAY_ORDER'),$trade_type);
        $data = $wx_class->chcek_notify_url();
        switch ($data['data']['attach']) {
            // 充值
            case 'recharge':
                $status = D('Pay')->wxPayRecharge($data);
                break;
            // 购买置顶套餐
            case 'top_package':
                    $status = D('Pay')->wxPayTopPackage($data);;
                    break;
            default:
                $status = false;
        }

        if ($status == 200) {
            $return = array(
                'return_code' => 'SUCCESS',
                'return_msg' => 'OK'
            );
        } else {
            $return = array(
                'return_code' => 'FAIL',
                'return_msg' => 'ERR'
            );
        }
        $returnXml = $wx_class->arrayToXml($return);
        header("Content-type:text/xml");
        echo($returnXml);
    }
}